import axios from 'axios'

const API_URL = process.env.VUE_APP_API_URL || 'http://localhost:3000/api'

const state = {
  categories: []
}

const mutations = {
  SET_CATEGORIES(state, categories) {
    state.categories = categories
  }
}

const actions = {
  // 获取所有宠物类别
  async fetchCategories({ commit, dispatch }) {
    try {
      dispatch('setLoading', true, { root: true })
      
      const response = await axios.get(`${API_URL}/pets/categories`)
      
      commit('SET_CATEGORIES', response.data)
      return response.data
    } catch (error) {
      // 不再设置全局错误状态，只记录到控制台
      console.error('加载分类数据失败:', error.message)
      return []
    } finally {
      dispatch('setLoading', false, { root: true })
    }
  }
}

const getters = {
  allCategories: state => state.categories,
  getCategoryById: state => id => state.categories.find(cat => cat.id === parseInt(id))
}

export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters
} 